#include "world_utils.h"

/*
 * Puts a blank dot in the place of a given dot.
 */
void clear_dot(dot* to_clear) {
	char blank[3] = BLANK_TYPE;
	to_clear->data = NULL;
	memcpy(&to_clear->type, &blank, 3);
}

/*
 * Puts a food dot on a given dot.
 */
void put_food(dot* put_food_on) {
	char food[3] = FOOD_TYPE;
	put_food_on->data = NULL;
	memcpy(&put_food_on->type, &food, 3);
}

/*
 * Initializes and puts a new AI with the given commands in a given dot.
 */
void put_new_ai(dot* put_ai, short* commands, int com_amount) {
	char ai_type[3] = AI_TYPE;
	put_ai->data = (ai_data*) malloc(sizeof(ai_data));
	put_ai->data->energy = DEFAULT_ENERGY;
	put_ai->data->com_count = com_amount;
	put_ai->data->last_turn = 0;
	put_ai->data->flag = FALSE;
	put_ai->data->last_look = -1;
	memcpy(put_ai->data->commands, commands, com_amount);
	memcpy(put_ai->type, &ai_type, 3);
}

/*
 * Frees all AI data memory in  the given world.
 */ 
void free_all_ai_data(dot* world, int world_size) {
	int i;
	for (i = 0; i < world_size; i++) {
		if (world[i].data != NULL) {
			free(world[i].data);
		}
	}
}

/*
 * Replaces a given dot with another.
 */
void replace_dot(dot* old_dot, dot* new_dot) {
	memcpy(new_dot, old_dot, sizeof(dot));
}

/*
 * Converts a given object to its respective dot type.
 */
int obj_to_type(int obj) {
	switch (obj) {
	case ENEMY:
		return AI_DOT;
	case FOOD:
		return FOOD_DOT;
	default:
		return -1;
	}
}

int get_opposite_direction(int direction) {
	switch (direction) {
	case LEFT:
		return RIGHT;
	case RIGHT:
		return LEFT;
	case UP:
		return DOWN;
	case DOWN:
		return UP;
	default:
		return -1;
	}
}